//
//  QXWelcomeVC.swift
//  QXWeibo
//
//  Created by QinXin on 15/6/26.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit
import SDWebImage

class QXWelcomeVC: UIViewController {

    var iconViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        if let url = NSURL(string: (sharedAccount?.avatar_large)!) {
            iconView.sd_setImageWithURL(url)
            messageLabel.text = "\(sharedAccount!.name!)，欢迎回来！"
            messageLabel.sizeToFit()
        }
        
    }
    
    lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = iv.image!.size.width * 0.5
        return iv
    }()
    
    lazy var messageLabel: UILabel = {
        let ml = UILabel()
        ml.text = " 欢迎归来！"
        ml.textAlignment = NSTextAlignment.Center
        ml.sizeToFit()
        return ml
    }()
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        self.messageLabel.alpha = 0
        self.iconViewBottomCons!.constant = -150;
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            self.messageLabel.alpha = 1.0
            self.view.layoutIfNeeded()
            }) { (_) -> Void in

                // true 表示跳转到主界面
                NSNotificationCenter.defaultCenter().postNotificationName(QXAppDelegateChangeRootViewControllerNotification, object: true)
        }
    }
    
    override func loadView() {
        view = UIView()
        let backgroundImgV = UIImageView(image: UIImage(named: "ad_background"))
        
        view.addSubview(backgroundImgV)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        backgroundImgV.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subViews]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subViews": backgroundImgV]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subViews]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subViews": backgroundImgV]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -200)
        view.addConstraint(iconViewBottomCons!)
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))

    }

}
